package server;

import java.net.Socket;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class HeartBitSender extends Thread{
	private List <Socket> connectedClients;
	private List <Socket> snapshotOfConnectedClients;
	private boolean worked;
	private int timeout;

	

	public HeartBitSender(List<Socket> connectedClients, int timeout) {
		super();
		this.connectedClients = connectedClients;
		this.timeout = timeout;
		this.worked=true;
	}

	@Override
	public void run() {
		while (worked){			
			snapshotOfConnectedClients=Collections.unmodifiableList(connectedClients);
			Iterator it=snapshotOfConnectedClients.iterator();
			while(it.hasNext()){
				sendHeartBit((Socket)it.next());
			}
			try {
				Thread.sleep(this.timeout);
			} catch (InterruptedException e) {
				System.out.println("Error in sleeping in heartbit sending");
			}
		}
	}

	private void sendHeartBit(Socket next) {
		//TODO Adding heart bit sending to socket
		
	}
	void turnOf(){
		this.worked=false;
	}
	
	
}
